package cn.com.dmg.myspringboot.textAndImg;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import cn.hutool.core.io.FileUtil;


/**
 * @author zhum
 * @ClassName GenerateImgUtil
 * @Description
 * @date 2024/10/12 16:06
 */
public class GenerateImgSimpleUtil {

    //public static String baseWorkPath = "C:\\Users\\13117\\Desktop\\1027-多个合集\\";
    //背景图地址
    public static String srcFilePath = "E:\\DeskFile\\标准文件夹\\标准背景图片\\";
    //工作文件夹
    public static String baseWorkPath = "E:\\DeskFile\\1103-合集\\";
    //结尾图片地址
    public static String endImgSrcPath = "E:\\DeskFile\\标准文件夹\\结尾图.png";

    public static List<String> gzhNameList = Arrays.asList("01-慢时光", "02-食疗", "03-上坡", "04-佳园", "05-能量棒", "06-奇妙屋");

    public static void main(String[] args) throws Exception {

        createImgAndContent();



    }

    /**
     * 图文生成
     * @author zhum
     * @date 2024/10/27 14:15
     * @return void
     */
    public static void createImgAndContent() {

        //背景图片（有多少张背景图片就生成多少套图文）
        List<File> srcFileList = FileUtil.loopFiles(new File(srcFilePath));

        //所有过度图片的文件夹
        List<String> dirFileNames = getDirFileNames();

        for (int i = 0; i < srcFileList.size(); i++) {
            File srcFile = srcFileList.get(i);



            for (String dirFileName : dirFileNames) {
                //过渡图片合集
                String transitionImgSrcPath = baseWorkPath + dirFileName + "\\";

                //某个文件夹下的所有图片
                List<File> transitionImgFileList = FileUtil.loopFiles(new File(transitionImgSrcPath), file -> file.getName().endsWith(".png") || file.getName().endsWith(".jpg"));
                //加上一张结尾图
                transitionImgFileList.add(new File(endImgSrcPath));

                //路径
                String imgAndContentPath = baseWorkPath + gzhNameList.get(i) + "//" + dirFileName+ "//";

                for (File transitionImgFile : transitionImgFileList) {
                    GenerateImgUtil.createImg(
                            srcFile.getAbsolutePath(),
                            imgAndContentPath + transitionImgFile.getName(),
                            Collections.singletonList(transitionImgFile),
                            null,
                            ImgLocationModeConstant.TWO_MODE_ONE
                    );
                }

                //移动文案
                File wenAnFile = new File(transitionImgSrcPath + "文案.txt");
                if (wenAnFile.exists()) {
                    FileUtil.move(wenAnFile, new File(baseWorkPath + dirFileName + ".txt"), true);
                }
            }



        }

        //删除所有素材图片
        for (String dirFileName : dirFileNames) {
            FileUtil.del(new File(baseWorkPath + dirFileName));
        }



    }

    public static List<String> getDirFileNames() {
        List<String> list = new ArrayList<>();

        // 创建File对象表示目录
        File directory = new File(baseWorkPath);

        // 确保目录存在且实际上是一个目录
        if (directory.exists() && directory.isDirectory()) {
            // 获取目录下所有子文件夹
            File[] subdirectories = directory.listFiles(File::isDirectory);

            // 遍历并打印子文件夹的名称
            if (subdirectories != null) {
                for (File subdirectory : subdirectories) {
                    list.add(subdirectory.getName());
                }
            }
        } else {
            System.out.println("Directory does not exist or is not a directory.");
        }
        return list;
    }
}
